﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeAnalyzer.Engine.Parser
{
    public class LanguageParser
    {

        public int TotalLinesCount
        {
            get;
            set;
        }

        public List<Command> Parse(string data, List<Command> language)
        {
            List<Command> parsedData = new List<Command>();
            string[] lines = data.Split('\n');
            int index = 0;
            TotalLinesCount += lines.Length;

            while (index < lines.Length)
            {
                Command control = FindControl(lines[index], language);
                if (control == null)
                {
                    index++;
                }
                else
                {
                    if (index + 1 < lines.Length)
                    {
                        if (control.HasOpenCommand == false && lines[index].IndexOf(control.OpenCommand) > -1)
                            index++;
                        else
                        {
                            index++;
                            parsedData.Add(TreeSearch(lines, language, control, ref index));
                        }
                    }
                }
            }

            return parsedData;
        }

        private Command TreeSearch(string[] lines, List<Command> language, Command parsedData, ref int index)
        {
            while (index < lines.Length)
            {
                if (lines[index].IndexOf(parsedData.CloseCommand) > -1)
                {
                    index++;
                    break;
                }
                else
                {
                    Command cmd = FindControl(lines[index], language);
                    if (cmd == null)
                    {
                        parsedData.CommandLineCount++;
                        index++;
                    }
                    else
                    {
                        if (parsedData.Level > cmd.Level && parsedData.Type != "class")
                            break;
                        else
                            if (parsedData.Type == "class" && cmd.Level > 1)
                            {
                                break;
                            }
                        if (index + 1 < lines.Length)
                        {
                            if (cmd.HasOpenCommand == false && lines[index].IndexOf(cmd.OpenCommand) > -1)
                            {
                                index++;
                            }
                            else
                            {
                                index++;
                                if (lines[index - 1].IndexOf(" " + cmd.OpenCommand + " ") > -1 && lines[index - 1].Substring(lines[index - 1].IndexOf(" " + cmd.OpenCommand + " "), cmd.OpenCommand.Length) != cmd.OpenCommand)
                                {

                                }
                                else
                                {
                                    parsedData.Children.Add(TreeSearch(lines, language, cmd, ref index));
                                    if (parsedData.Level > cmd.Level && parsedData.Type != "class")
                                        break;
                                    parsedData.Complexity += parsedData.Children[parsedData.Children.Count - 1].Complexity;
                                }
                                
                            }
                        }
                        else
                            break;
                        

                    }
                }

            }
            return parsedData;
        }

        private Command FindControl(string line, List<Command> language)
        {
            Command control = null;

            for (int iLanguage = 0; iLanguage < language.Count; iLanguage++)
            {
                if (line.IndexOf(language[iLanguage].Syntax + " ") == 0 || line.IndexOf(" " + language[iLanguage].Syntax + " ") > -1)
                {
                    control = new Command();

                    control.Name = language[iLanguage].Name;
                    control.OpenCommand = language[iLanguage].OpenCommand;
                    control.Syntax = language[iLanguage].Syntax;
                    control.CloseCommand = language[iLanguage].CloseCommand;
                    control.Complexity = language[iLanguage].Complexity;
                    control.Level = language[iLanguage].Level;


                    if (line.IndexOf(control.OpenCommand) > -1)
                        control.HasOpenCommand = true;
                    else
                        control.HasOpenCommand = false;
                }
            }

            return control;
        }
    }

    public class FileParser
    {
        public List<Command> Parse(string data, List<Command> file)
        {
            List<Command> parsedData = new List<Command>();



            return parsedData;
        }
    }
}
